<?php
/**
 * helper mailer is a simple send mail class
 * 	version:	0.1
 * 	update:		2009/7/28
 *
 */
class Helper_Mailer {
	public static $phpmailer;
	private static $save;
	
	public function __construct() {
		Mid::loadLib ( 'phpmailer:class.phpmailer' );
		$this->phpmailer = new PHPMailer ( );
		$phpmailer_dir = str_replace ( '.php', DIRECTORY_SEPARATOR, Mid::path ( '$:lib:phpmailer' ) );
		$this->phpmailer->PluginDir = $phpmailer_dir;
	}
	
	/**
	 * object phpmailer handle
	 *
	 * @return object phpmailer
	 */
	public function phpmailerInstance($new = FALSE) {
		if ($new === TRUE) {
			$this->phpmailer = new PHPMailer ( );
		}
		return $this->phpmailer;
	}
	
	/**
	 * create a mailer handle
	 *
	 * @param string $type (smtp|sendmail|mail)
	 * @param string $host (like:smtp.sina.com:443) 
	 * @param string $mail (like:corrie<corrie@sina.com>)
	 * @param string $user
	 * @param string $pswd
	 */
	public function login($type, $mail, $host = NULL, $user = NULL, $pswd = NULL) {
		$this->save ['type'] = $type;
		
		if ($mail) {
			$match = $this->parseAddress ( $mail );
			$this->save ['from'] = $match [0];
			$this->save ['fromname'] = $match [1];
		}
		
		if ($host === NULL) {
			$host = 'localhost';
		}
		$this->save ['host'] = $host;
		
		if ($user !== NULL || $pswd !== NULL) {
			if ($type == 'smtp') {
				$this->save ['smtpauth'] = true;
			}
			
			$this->save ['password'] = $pswd;
			$this->save ['username'] = $user;
		}
		return true;
	}
	
	/**
	 * create a mail
	 *
	 * @param string $type (like:TXT|HTML)
	 * @param string $subject mail subject
	 * @param string $body mail body
	 * @param string $attach mail attachment
	 */
	public function write($subject, $body, $attach = NULL, $type = 'HTML', $charset = 'utf-8', $encode = 'base64') {
		$this->save ['subject'] = $subject;
		$this->save ['body'] = $body;
		if ($attach !== NULL && ! empty ( $attach )) {
			$this->save ['attach'] = $attach;
		}
		if ($type != 'HTML') {
			$this->save ['ishtml'] = false;
		} else {
			$this->save ['ishtml'] = true;
		}
		$this->save ['charset'] = $charset;
		$this->save ['encode'] = $encode;
		return true;
	}
	
	public function sendto($to, $cc = NULL, $bcc = NULL, $rt = NULL) {
		$this->save ['to'] = $to;
		$this->save ['cc'] = $cc;
		$this->save ['bcc'] = $bcc;
		$this->save ['rt'] = $rt;
		$this->send ();
	}
	
	public function quicksend($to, $subject, $body) {
		$this->createmail ( $subject, $body );
		$this->sendmail ( $to );
	}
	
	private function send() {
		$mailer = $this->phpmailer;
		
		switch ($this->save ['type']) {
			case 'smtp' :
				$mailer->IsSMTP ();
				break;
			case 'sendmail' :
				$mailer->IsSENDMAIL ();
				break;
			case 'mail' :
				$mailer->IsMAIL ();
				break;
		}
		
		$mailer->Host = $this->save ['host'];
		$mailer->From = $this->save ['from'];
		$mailer->FromName = $this->save ['fromname'];
		$mailer->Username = $this->save ['username'];
		$mailer->Password = $this->save ['password'];
		$mailer->CharSet = $this->save ['charset'];
		$mailer->Encoding = $this->save ['encode'];
		
		if ($this->save ['smtpauth']) {
			$mailer->SMTPAuth = true;
		}
		
		$mailer->Subject = $this->save ['subject'];
		$mailer->Body = $this->save ['body'];
		
		if (isset ( $this->save ['attach'] ) && ! empty ( $this->save ['attach'] )) {
			foreach ( $this->save ['attach'] as $v ) {
				$mailer->AddAttachment ( $v );
			}
		}
		
		if (! $this->save ['ishtml']) {
			$mailer->IsHTML ( false );
		} else {
			$mailer->IsHTML ( true );
		}
		
		if (is_array ( $this->save ['to'] ) && ! empty ( $this->save ['to'] )) {
			foreach ( $this->save ['to'] as $address ) {
				$match = $this->parseAddress ( $address );
				$mailer->AddAddress ( $match [0], $match [1] );
			}
		} else {
			$match = $this->parseAddress ( $this->save ['to'] );
			$mailer->AddAddress ( $match [0], $match [1] );
		}
		
		if (isset ( $this->save ['cc'] ) && is_array ( $this->save ['cc'] ) && ! empty ( $this->save ['cc'] )) {
			foreach ( $this->save ['cc'] as $address ) {
				$match = $this->parseAddress ( $address );
				$mailer->AddCC ( $match [0], $match [1] );
			}
		} elseif ($this->save ['cc'] !== NULL) {
			$match = $this->parseAddress ( $this->save ['cc'] );
			$mailer->AddCC ( $match [0], $match [1] );
		}
		
		if (isset ( $this->save ['bcc'] ) && is_array ( $this->save ['bcc'] ) && ! empty ( $this->save ['bcc'] )) {
			foreach ( $this->save ['bcc'] as $address ) {
				$match = $this->parseAddress ( $address );
				$mailer->AddBCC ( $match [0], $match [1] );
			}
		} elseif ($this->save ['bcc'] !== NULL) {
			$match = $this->parseAddress ( $this->save ['bcc'] );
			$mailer->AddBCC ( $match [0], $match [1] );
		}
		
		if (isset ( $this->save ['rt'] ) && is_array ( $this->save ['rt'] ) && ! empty ( $this->save ['rt'] )) {
			foreach ( $this->save ['rt'] as $address ) {
				$match = $this->parseAddress ( $address );
				$mailer->AddReplyTo ( $match [0], $match [1] );
			}
		} elseif ($this->save ['rt'] !== NULL) {
			$match = $this->parseAddress ( $this->save ['rt'] );
			$mailer->AddReplyTo ( $match [0], $match [1] );
		}
		$mailer->Send ();
	}
	
	private function clear() {
		$this->phpmailer->ClearAllRecipients ();
	}
	
	private function reset() {
		$this->phpmailer ( TRUE );
	}
	
	private function parseAddress($str) {
		if (preg_match ( '/(.*)<(.*)>/', $str, $match )) {
			$address = $match [2];
			$name = $match [1] != '' ? $match [1] : '';
		} else {
			$address = $str;
			$name = '';
		}
		//exit($name . $address);
		return array ($address, $name );
	}
}
?>